// index.js
// 引入SDK核心类
var R_htmlToWxml = require('../../resource/js/htmlToWxml.js');//引入公共方法
var imageUtil = require('../../resource/js/images.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: {},
    autoplay: true,
    interval: 3000,
    duration: 1000,
    title: '',
    address: '',
    lat: 0,
    lng: 0,
    id: 0,
    title: '',
    showmsg: true
  }
  , imageLoad: function (e) {
    var imageSize = imageUtil.imageUtil(e)
    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {

    wx.setNavigationBarTitle({
      title: wx.getStorageSync('companyinfo').name,
    })


    var that = this;
    if (that.data.id > 0) {
      var id = that.data.id;
    } else {
      var id = e.id;
      that.data.id = e.id;
    }
    that.checkuser({
      doServices: function () {

        that.getlethousedetail();
        
      },
      doElseServices: function () {
    
        //初始化导航数据

        that.getlethousedetail();


      }


    });


  },
  goMap: function (e) {
    var that = this;
    console.log('ffffff');
    wx.openLocation({
      latitude: parseFloat(that.data.lat),
      longitude: parseFloat(that.data.lng),
      scale: 18,
      name: that.data.title,
      address: that.data.address
    })
  },
  doCall: function (e) {

 var that = this;

    var tel = e.currentTarget.dataset.tel;

    wx.makePhoneCall({
      phoneNumber: tel, //此号码并非真实电话号码，仅用于测试
      success: function (res) {
        console.log("拨打电话成功！")
      },
      fail: function (res) {
        console.log(res);
        console.log("拨打电话失败！")
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getlethousedetail:function(){
    var that = this;
    var userinfo = wx.getStorageSync('userInfo');
    app.util.request({
      'url': 'entry/wxapp/getlethousedetail',
      data: { id: that.data.id, sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid},
      success: function (res) {

        if (!res.data.message.errno) {
          var houseDetail = R_htmlToWxml.html2json(res.data.data.list.content);
          that.data.title = res.data.data.list.title;
          that.data.address = res.data.data.list.address;
          that.data.lat = res.data.data.list.lat;
          that.data.lng = res.data.data.list.lng;

          wx.setNavigationBarTitle({
            title: that.data.title + '-' + wx.getStorageSync('companyinfo').name,
          })
          that.setData({
            data: res.data.data.list,
            piclist: res.data.data.piclist,
            housepic: res.data.data.housepic,
            houseplan: res.data.data.houseplan,
            content: houseDetail,
            ispay:res.data.data.ispay
          })
        }
      },
      complete: function () {


        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh();
      }
    });
    //that.setData({ userinfo: userinfo })

  },


  onShareAppMessage() {
    return {
      title: this.data.title + '-' + wx.getStorageSync('companyinfo').name,
      path: '/weixinmao_house/pages/lethousedetail/index?id=' + this.data.id
    }
  }, checkuser: function (options) {


    var that = this;
    var options = options;
    var userinfo = wx.getStorageSync('userInfo');
    console.log(userinfo);
    if (!userinfo) {
      app.util.getUserInfo(
        function (userinfo) {
          that.getlethousedetail();
        }



      );
      return false;
    } else {
      if (!userinfo.memberInfo.uid) {
        app.util.getUserInfo();
        return false;
      } else {

        app.util.request({
          'url': 'entry/wxapp/checkuserinfo',
          data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid },
          success: function (res) {
            console.log('payyyy');
            if (res.data.data.error == 0) {

              options.doServices();

            } else if (res.data.data.error == 2) {
              // console.log('payyyy');
              options.doElseServices();

            } else {




            }

          }
        });

      }
    }

  }, pay: function (e) {
    var that = this;
    that.checkuser({
      doServices: function () {

        that.data.showmsg = false;
        that.setData({
          showmsg: that.data.showmsg
        })
      },
      doElseServices: function () {

        var pid = that.data.id;
        var userinfo = wx.getStorageSync('userInfo');
        var ordertype='lethouse';
        wx.showModal({
          title: '确认支付',
          content: '确认支付定金？',
          success: function (res) {
            if (res.confirm) {
              app.util.request({
                'url': 'entry/wxapp/pay',
                data: { ordertype: ordertype, pid: pid, sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid },
                success: function (res) {
                  console.log(res);
                  if (res.data && res.data.data) {
                    wx.requestPayment({
                      'timeStamp': res.data.data.timeStamp,
                      'nonceStr': res.data.data.nonceStr,
                      'package': res.data.data.package,
                      'signType': 'MD5',
                      'paySign': res.data.data.paySign,
                      'success': function (res) {
                        //支付成功后，系统将会调用payResult() 方法，此处不做支付成功验证，只负责提示用户
                        console.log(res);
                        that.setData({
                          ispay: 1
                        })

                      },
                      'fail': function (res) {
                        //支付失败后，
                      }
                    })
                  }

                },
                fail: function (res) {
                  console.log(res);
                }

              })



            }
          }

        })

      }


    });



  },
  saveuserinfo: function (e) {
    var that = this;
    var name = e.detail.value.name;
    var tel = e.detail.value.tel;
    that.data.showmsg = true;
    var userinfo = wx.getStorageSync('userInfo');

    if (name == "") {
      wx.showModal({
        title: '提示',
        content: '请填写您的姓名',
        showCancel: false
      })
      return;
    }
    if (tel == "") {
      wx.showModal({
        title: '提示',
        content: '请填写您的手机号',
        showCancel: false
      })
      return;
    }


    app.util.request({
      'url': 'entry/wxapp/saveuserinfo',
      data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid, name: name, tel: tel },
      success: function (res) {


        if (res.data.errno != 0) {
          // 登录错误 
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: false
          })
          return;
        } else {

          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          })
          that.setData({

            showmsg: that.data.showmsg
          })
        }



      }
    });




  }, closemsg: function (e) {

    this.data.showmsg = true;
    this.setData({
      showmsg: this.data.showmsg
    })

  },

})